import {
    GetList,
    GetDetail,
    GetLog,
    AgreeRefund,
    RefuseRefund,
    AgreeReturnAddr,
    AgreeReturnGoods,
    ComfirmReturn,
    RefuseReturn,
    AgreeOffRefund,
    ComfirmOffReturn,
    ExportList
} from '@/services/order/orderRight';
import {catchException} from '@/utils/utils';
export default {
    namespace: 'orderRight',

    state: {
        rightList: [],
        rightDetail: {},
        rightLog: {},
    },

    effects: {

        // 获取订单列表
        *GetList({payload, callBack}, {call, put}) {
            const data = yield call(GetList, payload);
            yield catchException(function* (){
                if(data.Code === 0) {
                    yield put({
                        type: 'saveGetList',
                        payload: {
                            dataList: data.Data,
                        },
                    });
                    callBack && callBack.success(data);
                } else {
                    callBack && callBack.error(data)
                }
            })             
        },

        
        // 通过维权Id获取订单详情
        *GetDetail({payload, callBack}, {call, put}) {
            const data = yield call(GetDetail, payload);
            yield catchException(function* (){
                if(data.Code === 0) {
                    yield put({
                        type: 'saveGetDetail',
                        payload: {
                            dataList: data.Data,
                        },
                    });
                    callBack && callBack.success(data);
                } else {
                    callBack && callBack.error(data)
                }
            })             
        },

        // 获取退款协商记录
        *GetLog({payload, callBack}, {call, put}) {
            const data = yield call(GetLog, payload);
            yield catchException(function* (){
                if(data.Code === 0) {
                    yield put({
                        type: 'saveGetLog',
                        payload: {
                            dataList: data.Data,
                        },
                    });
                    callBack && callBack.success(data);
                } else {
                    callBack && callBack.error(data)
                }
            })             
        },

         //同意退款申请(仅退款)
         *AgreeRefund({payload, callBack}, {call, put}) {
            const data = yield call(AgreeRefund, payload);
            yield catchException(function* (){
                if(data.Code === 0) {
                    callBack && callBack.success(data);
                } else {
                    callBack && callBack.error(data)
                }
            })            
          },

           //线下同意退款申请(仅退款)
         *AgreeOffRefund({payload, callBack}, {call, put}) {
            const data = yield call(AgreeOffRefund, payload);
            yield catchException(function* (){
                if(data.Code === 0) {
                    callBack && callBack.success(data);
                } else {
                    callBack && callBack.error(data)
                }
            })            
          },

          
           //订单导出
         *ExportList({payload, callBack}, {call, put}) {
            const data = yield call(ExportList, payload);
            yield catchException(function* (){
                if(data.Code === 0) {
                    callBack && callBack.success(data);
                } else {
                    callBack && callBack.error(data)
                }
            })             
          },


          //同意退货，发送退货地址（新增收货地址）
         *AgreeReturnAddr({payload, callBack}, {call, put}) {
            const data = yield call(AgreeReturnAddr, payload);
            yield catchException(function* (){
                if(data.Code === 0) {
                    callBack && callBack.success(data);
                } else {
                    callBack && callBack.error(data)
                }
            })             
          },

          // 同意退货，发送退货地址
          *AgreeReturnGoods({payload, callBack}, {call, put}) {
            const data = yield call(AgreeReturnGoods, payload);
            yield catchException(function* (){
                if(data.Code === 0) {
                    callBack && callBack.success(data);
                } else {
                    callBack && callBack.error(data)
                }
            })             
          },

            // 确认收到退货
            *ComfirmReturn({payload, callBack}, {call, put}) {
                const data = yield call(ComfirmReturn, payload);
                yield catchException(function* (){
                    if(data.Code === 0) {
                        callBack && callBack.success(data);
                    } else {
                        callBack && callBack.error(data)
                    }
                })                 
              },

              
            // 线下确认收到退货
            *ComfirmOffReturn({payload, callBack}, {call, put}) {
                const data = yield call(ComfirmOffReturn, payload);
                yield catchException(function* (){
                    if(data.Code === 0) {
                        callBack && callBack.success(data);
                    } else {
                        callBack && callBack.error(data)
                    }
                })                
              },


        //拒绝
         *RefuseRefund({payload, callBack}, {call, put}) {
            const data = yield call(RefuseRefund, payload);
            yield catchException(function* (){
                if(data.Code === 0) {
                    callBack && callBack.success(data);
                } else {
                    callBack && callBack.error(data)
                }
            })             
          },

            //拒绝退货申请
         *RefuseReturn({payload, callBack}, {call, put}) {
            const data = yield call(RefuseReturn, payload);
            yield catchException(function* (){
                if(data.Code === 0) {
                    callBack && callBack.success(data);
                } else {
                    callBack && callBack.error(data)
                }
            })             
          },


    },

    reducers: {
        saveGetList(state, {payload}) {
            return {
                ...state,
                rightList: payload.dataList,
            };
        },
        saveGetDetail(state, {payload}) {
            return {
                ...state,
                rightDetail: payload.dataList,
            };
        },
        saveGetLog(state, {payload}) {
            return {
                ...state,
                rightLog: payload.dataList,
            };
        },
    }
}